{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 20\n",
    "\n",
    "### **Question:**\n",
    "\n",
    "> **_Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n._**\n",
    "\n",
    "---\n",
    "\n",
    "### Hints:\n",
    "\n",
    "> **_Consider use class, function and comprehension._**\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "\n",
    "**Solution: Python 3**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Solution by: ShalomPrinz\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "class MyGen:\n",
    "    def by_seven(self, n):\n",
    "        for i in range(0, int(n / 7) + 1):\n",
    "            yield i * 7\n",
    "\n",
    "\n",
    "for i in MyGen().by_seven(int(input(\"Please enter a number... \"))):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Solution by: Seawolf159\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "class Divisible:\n",
    "    def by_seven(self, n):\n",
    "        for number in range(n + 1):\n",
    "            if number % 7 == 0:\n",
    "                yield number\n",
    "\n",
    "\n",
    "divisible = Divisible()\n",
    "generator = divisible.by_seven(int(input(\"Please insert a number. --> \")))\n",
    "for number in generator:\n",
    "    print(number)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# Question 21\n",
    "\n",
    "### **Question:**\n",
    "\n",
    "> **_A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:_**\n",
    "\n",
    "```\n",
    "UP 5\n",
    "DOWN 3\n",
    "LEFT 3\n",
    "RIGHT 2\n",
    "```\n",
    "> **_The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer._**\n",
    "> **_Example:_**\n",
    "> **_If the following tuples are given as input to the program:_**\n",
    "\n",
    "```\n",
    "UP 5\n",
    "DOWN 3\n",
    "LEFT 3\n",
    "RIGHT 2\n",
    "```\n",
    "\n",
    "> **_Then, the output of the program should be:_**\n",
    "\n",
    "```\n",
    "2\n",
    "```\n",
    "---\n",
    "\n",
    "### Hints:\n",
    "\n",
    "> **_In case of input data being supplied to the question, it should be assumed to be a console input.Here distance indicates to euclidean distance.Import math module to use sqrt function._**\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "\n",
    "**Solutions:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "x, y = 0, 0\n",
    "while True:\n",
    "    s = input().split()\n",
    "    if not s:\n",
    "        break\n",
    "    if s[0] == \"UP\":  # s[0] indicates command\n",
    "        x -= int(s[1])  # s[1] indicates unit of move\n",
    "    if s[0] == \"DOWN\":\n",
    "        x += int(s[1])\n",
    "    if s[0] == \"LEFT\":\n",
    "        y -= int(s[1])\n",
    "    if s[0] == \"RIGHT\":\n",
    "        y += int(s[1])\n",
    "        # N**P means N^P\n",
    "dist = round(\n",
    "    math.sqrt(x ** 2 + y ** 2)\n",
    ")  # euclidean distance = square root of (x^2+y^2) and rounding it to nearest integer\n",
    "print(dist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
